package com.example.day02.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.day02.R;
import com.example.day02.bean.FuLiBean;

import java.util.List;

public class FuLiAdapter extends RecyclerView.Adapter<FuLiAdapter.ViewHolder> {
    private Context context;
    private List<FuLiBean.ResultsBean> datas;
    private OnContextClickListener onContextClickListener;
    public interface OnContextClickListener{
        void oncontextcliklistener( int pos);
    }

    public FuLiAdapter(Context context, List<FuLiBean.ResultsBean> datas) {
        this.context = context;
        this.datas = datas;
    }

    public void setOnContextClickListener(OnContextClickListener onContextClickListener) {
        this.onContextClickListener = onContextClickListener;
    }

    @NonNull
    @Override
    public FuLiAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.fuliitime, parent, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull FuLiAdapter.ViewHolder holder, int position) {
        FuLiBean.ResultsBean resultsBean = datas.get(position);
        Glide.with(context).load(resultsBean.getUrl()).into(holder.image);
        holder.text.setText(resultsBean.getDesc());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onContextClickListener.oncontextcliklistener(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView image;
        TextView text;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            image=itemView.findViewById(R.id.image);
            text=itemView.findViewById(R.id.text);
        }
    }
}
